﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.Finance.EntityFrameworkCore;
using mozhi.smarterp.Finance.Finances.Repository;
using mozhi.smarterp.Finance.Querys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.Finance.Finances
{
    public class EndPeriodProcessItemRepository : EfCoreRepository<FinanceDbContext, EndPeriodProcessItem , Guid>, IEndPeriodProcessItemRepository
    {
        public EndPeriodProcessItemRepository(IDbContextProvider<FinanceDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<List<EndPeriodProcessItem>> FindAsync(PageQuery query)
        {
            var dbQuery = query.IncludeDetail ? DbContext.Set<EndPeriodProcessItem>().Include(x => x.Settings).AsNoTracking() : DbContext.Set<EndPeriodProcessItem>().AsNoTracking();

            var items = await dbQuery.
                WhereIf(query.Id.HasValue, x => x.Id == query.Id).
                WhereIf(!query.Keyword.IsNullOrEmpty(), x => x.Name != null && x.Name.Contains(query.Keyword))
                .Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize)
                .OrderBy(x=>x.Index).ToListAsync();

            return items;
        }

        public async Task<EndPeriodProcessItem> FindAsync(Guid id, bool includeDetails = true, CancellationToken cancellationToken = default)
        {
            var dbQuery = includeDetails ? DbContext.Set<EndPeriodProcessItem>().Include(x => x.Settings).AsNoTracking() : DbContext.Set<EndPeriodProcessItem>().AsNoTracking();

            return await dbQuery.FirstOrDefaultAsync(x => x.Id == id);
        }

        public async Task<EndPeriodProcessItem> GetAsync(Guid id, bool includeDetails = true, CancellationToken cancellationToken = default)
        {
            return await DbContext.Set<EndPeriodProcessItem>().Include(x => x.Settings).FirstOrDefaultAsync(x => x.Id == id);
        }

        public async Task<int> GetCountAsync(PageQuery query)
        {
            var count = await DbContext.Set<EndPeriodProcessItem>().AsNoTracking().
                WhereIf(query.Id.HasValue, x => x.Id == query.Id).
                WhereIf(!query.Keyword.IsNullOrEmpty(), x => x.Name != null && x.Name.Contains(query.Keyword))
                .CountAsync();

            return count;
        }

        public async Task<List<EndPeriodProcessItem>> InsertAsync(List<EndPeriodProcessItem> items)
        {
            if (items == null)
                throw new ArgumentNullException(nameof(items));

            foreach (var item in items)
            {
                DbContext.Set<EndPeriodProcessItem>().Add(item);
            }

            await DbContext.SaveChangesAsync();

            return items;
        }


        public async Task<bool> DeleteAsync(List<EndPeriodProcessItem> items)
        {
            items.ForEach(item =>
            {
                DbContext.Set<EndPeriodProcessItem>().Remove(item);
            });

            await DbContext.SaveChangesAsync();

            return true;
        }
    }
}
